package com.robot.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.robot.model.Protocol;
import com.robot.vo.ResponseMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/8/18
 * Time: 10:45
 * PROJECT：robot
 * PACKAGE: com.robot
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Component
@Slf4j
public class HttpClientUtil {


    public ResponseMessage invoke(Protocol http){
        HttpClient httpClient = HttpClients.createDefault();
        HttpClientContext context=new HttpClientContext();
        URIBuilder uriBuilder = null;
        try {
            uriBuilder = new URIBuilder(http.getInterfaceName());
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        String param =http.getParam();
        if(!StringUtils.isEmpty(param)){
            List<Map<String,String>> list = JSON.parseObject(param,new TypeReference<List<Map<String,String>>>(){});
            for(Map<String,String> map:list){
                uriBuilder.addParameter(map.get("key"),map.get("value"));
            }
        }
        String interfaceMethod=http.getInterfaceMethod();
        HttpRequestBase httpRequestBase =null;
        if(interfaceMethod.equalsIgnoreCase(HttpMethod.GET.name())){
            try {
                httpRequestBase = new HttpGet(uriBuilder.build());
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }else {
            try {
                httpRequestBase =new HttpPost(uriBuilder.build());
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            String body =http.getBody();
            if(!StringUtils.isEmpty(body)){
                StringEntity stringEntity=new StringEntity(body,http.getEncoding());
                ((HttpPost)httpRequestBase).setEntity(stringEntity);
            }
        }
        String header =http.getHeader();
        if(!StringUtils.isEmpty(header)){
            List<Map<String,String>> list = JSON.parseObject(header,new TypeReference<List<Map<String,String>>>(){});
            for(Map<String,String> map:list){
                httpRequestBase.setHeader(map.get("key"),map.get("value"));
            }
        }
        String encoding =http.getEncoding();
        httpRequestBase.setHeader("charset",encoding);
        RequestConfig.Builder config =RequestConfig.custom();
        config.setSocketTimeout(http.getTimeout());
        httpRequestBase.setConfig(config.build());
        try {
            HttpResponse httpResponse  = httpClient.execute(httpRequestBase,context);
            String result = EntityUtils.toString(httpResponse.getEntity());
            return ResponseMessage.successResponse(result);
        } catch (IOException e) {
            log.error("调用http请求出现异常",e);
            return ResponseMessage.errorResponse("请求出现异常");
        }
    }
}
